package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1。连接   2.准备队列  3. 接受消息
        //2,创建helloworld队列
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        //后面与服务器进行通信 都是通信通道来进行
        Channel c = con.createChannel();

        //2，创建helloworld的队列
        //在rabbitmq服务器上创建队列 如果队列已经存在，服务器不会重复创建
        c.queueDeclare("helloworld",false,false,false,null);
        System.out.println("消息发送完成");

        //创建回调对象
        //3,接受消息 ，向helloworld队列发送信息
        DeliverCallback deliverCallback= new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] a = delivery.getBody();
                String msg = new String(a);
                System.out.println("收到："+msg);
            }
        };
        CancelCallback cancelCallback= new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {
                
            }
        };
        //接受消息， 交给回调参数进行处理
        /*
        * autoAck是否自动确认
        * */
        c.basicConsume("helloworld", true, deliverCallback, cancelCallback);

        
    }
}